<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


$THEME->addBreadcrumb($this->adminUrl.'/import/import-page', $this->lang('ap_title_export_import-page'));

$BWT = JFX::registry('ThirdParty_BWT');
$MTF = JFX::registry('ThirdParty_MTF');



$VIEW->addJs('
$(document).ready(function(){
    $("#jfxcore-import-page-tree-from").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });
    $("#jfxcore-import-page-tree-from a").click(function(){
        $("#jfx-parent-id-from").get(0).value = this.id.replace("jfx-page_from_", "");
        $("#jfx-disabled-parent-from").get(0).value = $(this).text();
    });
});
');











if(Antz_IntelliForm::submitted('import-page')){
    $error = false;

    $parentid = (int) post('parentFrom');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$parentid}'")==0){
        $error = true;
        JFX::addError('Invalid parent id');
    }

    if(!file_exists($_FILES['file']['tmp_name'])){
        $error = true;
        JFX::addError('File upload failed');
    }

    if(!$error){
        //$fc = file_get_contents($_FILES['file']['tmp_name']);
        //$page = unserialize($BWT->inverse($MTF->decode($fc)));
        //$page = unserialize($fc);

        $FILE = new JFX_File;
        if(!$FILE->load($_FILES['file']['tmp_name'])){
            $error = true;
            JFX::addError('Invalid file type');
        }else{
            if($FILE->get('type') != 'page_export'){
                $error = true;
                JFX::addError('Not a valid page export');
            }else{
                $page = unserialize($FILE->get('content'));
                if(!is_array($page)){
                    $error = true;
                    JFX::addError('Could not load file contents');
                }
            }

        }

        if(!$error){
            $this->importPage($FILE, $parentid);
        }

    }


}





$pagesRecursiveFrom = JFX::getPageTree(0, $USER->workspace);

$treeFrom = '<ul id="jfxcore-import-page-tree-from">'.JFX_ImportPage_MakeTree($pagesRecursiveFrom, 'jfx-page_from_', $USER->workspace).'</ul>';



    $parentFrom = array(
        'name' => 'parentFrom',
        'value' => post('parentFrom', 0),
        'type' => 'hidden',
        'id' => 'jfx-parent-id-from',
        'label' => ''
    );
    $parentListFrom = array(
        'type' => 'longtag',
        'tag' => 'div',
        'class' => 'form-field',
        'id' => 'jfx-parent-page-tree-from',
        'content' => '<label>Page to copy into</label>
<div class="input-area">'.$treeFrom.'</div>'
    );
    $selectedParentFrom = array(
        'name' => 'disabled_parent_from',
        'value' => post('disabled_parent_from'),
        'type' => 'text',
        'id' => 'jfx-disabled-parent-from',
        'extra' => array(
            array(
                'command' => 'addAttribute',
                'params' => array(
                    '"disabled"', '"disabled"'
                )
            )
        ),
        'label' => '&nbsp;'
    );









$formFields = array(
    array(
        'type' => 'file',
        'name' => 'file',
        'value' => '',
        'label' => 'File to import'
    ),
    $parentFrom,
    $parentListFrom,
    $selectedParentFrom,
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => 'Import',
        'label' => '&nbsp;'
    )
);

echo JFX::makeRapidForm($formFields, 'Import pages', 'import-page');




















